import logging
from pathlib import Path


class Logger:
    def __init__(self, name, log_file="model.log", level=logging.INFO):
        self.logger = logging.getLogger(name)
        self.logger.setLevel(level)

        # 避免重复添加处理器
        if not self.logger.handlers:
            formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")

            # 控制台处理器
            console_handler = logging.StreamHandler()
            console_handler.setFormatter(formatter)

            # 文件处理器
            log_dir = Path("logs")
            log_dir.mkdir(exist_ok=True)
            file_handler = logging.FileHandler(log_dir.joinpath(log_file), encoding="utf-8")
            file_handler.setFormatter(formatter)

            self.logger.addHandler(console_handler)
            self.logger.addHandler(file_handler)

    def get_logger(self):
        return self.logger